package com.xuecheng.teaching.service.impl;


import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.commons.constants.CommonConstants;
import com.xuecheng.commons.constants.OrdersConstants;
import com.xuecheng.commons.constants.WorkRecordCorrectionStatusEnum;
import com.xuecheng.commons.model.dto.PageRequest;
import com.xuecheng.commons.model.dto.PageWork;
import com.xuecheng.commons.model.dto.WorkDto;
import com.xuecheng.commons.model.dto.WorkQuestDto;
import com.xuecheng.commons.model.vo.CoursePubVo;
import com.xuecheng.commons.model.vo.PageResponseResult;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.model.vo.WorkVo;
import com.xuecheng.commons.utils.AuthInfoHolder;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.content.api.ContentClient;
import com.xuecheng.content.domain.CourseBase;
import com.xuecheng.content.domain.TeachplanWork;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.TeachplanWorkService;
import com.xuecheng.teaching.domain.Work;
import com.xuecheng.teaching.domain.WorkRecord;
import com.xuecheng.teaching.mappers.WorkMapper;
import com.xuecheng.teaching.service.WorkRecordService;
import com.xuecheng.teaching.service.WorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 作业 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-10-10
 */
@Service
public class WorkServiceImpl extends ServiceImpl<WorkMapper, Work> implements WorkService {
    @Autowired
    private WorkRecordService workRecordService;
    //分页条件查询课程计划作业列表
    @Override
    public ResponseResult<WorkVo> findAll(PageRequest request) {
        Page<Work> workPage = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<Work> qw = new LambdaQueryWrapper<>();
        qw.eq(Work::getCompanyId, AuthInfoHolder.getCompanyId());
        Page<Work> page = this.page(workPage, qw);
        long total = page.getSize();
        List<Work> works = page.getRecords();
        List<WorkVo> workVos = BeanHelper.copyWithCollection(works, WorkVo.class);

        for (WorkVo workVo : workVos) {
            for (Work work : works) {
                LambdaQueryWrapper<WorkRecord> wrQw = new LambdaQueryWrapper<>();
                wrQw.eq(WorkRecord::getWorkId, work.getId());
                //wrQw.eq(WorkRecord::getStatus, WorkRecordCorrectionStatusEnum.RECORD_CORRECT_PENDED);
                List<WorkRecord> list = workRecordService.list(wrQw);
                Integer num = 0;
                for (WorkRecord workRecord : list) {
                    if (workRecord.getWorkId().equals(work.getId())) {
                        num++;
                        Integer id = Integer.valueOf(work.getId().toString());
                        workVo.setWorkId(id);
                        workVo.setChangeDate(work.getChangeDate().toString());
                        workVo.setUserNum(num);
                    }
                }


            }
        }

        return new PageResponseResult(total,workVos);
    }
//新增作业
    @Override
    public ResponseResult saveWork(WorkQuestDto dto) {
        LambdaQueryWrapper<Work> qw = new LambdaQueryWrapper<>();
        qw.eq(Work::getId, dto.getWorkId());
        Work work = this.getOne(qw);
        //Work work = new Work();
        if (work != null) {
            Long id = work.getId();
            work.setId(id);
            work.setTitle(dto.getTitle());
            work.setQuestion(dto.getQuestion());
            this.updateById(work);
        } else {
            work = new Work();
            /*Long id = Long.valueOf(new SimpleDateFormat("HHmmssSSS").format(new Date()));
            work.setId(id);*/
            work.setChangeDate(new Date());
            work.setCompanyId(AuthInfoHolder.getCompanyId());
            work.setTitle(dto.getTitle());
            work.setQuestion(dto.getQuestion());
            work.setStatus(CommonConstants.USING_FLAG);
            this.save(work);
        }
        return ResponseResult.okResult();
    }



}
